<?php


namespace Weicot\Cms\Controller;

use think\Db;
use Weicot\Base\Model\Help;
use Weicot\Core\Controller;


class Content extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new \Weicot\Cms\Model\Content();
    }


    /***
     * index 页面
     */
    public function index()
    {
        /***
         * 此方法 支持数据传递  支持添加多个模板 支持添加 js 或 CSS 文件等
         */
        $this->setView()->addTemplate(
            array(
                "blockName" => "Cms.Common",
                "templatePath" => "Cms/View/Content/index",
            )
        )->setLayout()->toHtml();
    }


    public function page()
    {
        $filter = $this->post();
        $query = Db::table($this->model->table)->field($this->model->field);
        if (isset($filter['status']) && $filter['status'] != '') {
            $query->where('status', '=', $filter['status']);
        }
        if (isset($filter['title']) && $filter['title'] != '') {
            $query->where('title', 'like', '%' . $filter['title'] . '%');
        }
        if (isset($filter['description']) && $filter['description'] != '') {
            $query->where('description', 'like', '%' . $filter['description'] . '%');
        }
        $count = $query->count();
        $list = $query->page($this->getPage(), $this->getLimit())->order('id', 'desc')->select();
        foreach ($list as &$item) {
            $item['created'] = $item['created'] >= 1 ? date("Y-m-d H:i:s", $item['created']) : "";
            $item['updated'] = $item['updated'] >= 1 ? date("Y-m-d H:i:s", $item['updated']) : "";
            $item['status_name'] = Help::getArrayValue(\Weicot\Cms\Model\Content::$status, $item['status']);
        }
        $this->success('分页数据', 1111, [
            'count' => $count,
            'list' => $list
        ]);
    }


    /***
     * 添加函数
     */
    public function add()
    {
        $data = $this->post();
        if (!empty($data)) {
            if (isset($data['title']) && isset($data['description']) && !empty($data['title'])) {
                $save = Help::getFormatArrayData($data, $this->model->field, false);
                $save['created'] = time();
                $save['updated'] = time();
                Db::table($this->model->table)->insert($save);
                $id = Db::getLastInsID();
                if ($id) {
                    $this->success('添加成功', 1111, ['id' => $id]);
                }
                $this->error('添加失败');
            }
        }
        $this->error('提交数据不全');
    }


    /***
     * 编辑函数
     */
    public function edit()
    {
        $data = $this->post();
        if (!empty($data)) {
            if (isset($data['id']) && isset($data['title']) && !empty($data['title']) && !empty($data['id'])) {
                $save = Help::getFormatArrayData($data, $this->model->field, false);
                $id = $save['id'];
                unset($save['id']);
                $save['updated'] = time();
                if (Db::table($this->model->table)->where('id', '=', $id)->update($save)) {
                    $this->success('修改成功', 1111, ['id' => $id]);
                };
                $this->error('修改失败');
            }
        }
        $this->error('提交数据不全');
    }


    /***
     * 删除
     * @throws \think\Exception
     * @throws \think\db\exception\PDOException
     */
    function del()
    {
        $id = $this->post('id');
        if ($id) {
            $status = Db::table($this->model->table)
                ->where('id', '=', $id)
                ->delete();
            if ($status) {
                $this->success('删除成功', 1111);
            };
        }

        $this->error('删除失败');
    }


    public function routeDetails()
    {
        var_dump(\Weicot\Core\Run::routeDetails());
    }

    public function detail()
    {
        parent::detail(); // TODO: Change the autogenerated stub
    }

    public function quickEdit()
    {
        parent::quickEdit(); // TODO: Change the autogenerated stub
    }
}
